package club.xyes.remotedisplay.scheduled;

import club.xyes.remotedisplay.jni.OLEDScreen;
import club.xyes.remotedisplay.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 2018/11/14 Create by 郭文梁
 * TimeRefreshScheduler
 * 定时刷新显示时间
 *
 * @author 郭文梁
 * @date 2018/11/14
 */
@Component
@Slf4j
public class TimeRefreshScheduler {
    private String lastTimeStr;
    private String lastDateStr;
    @Autowired
    private OLEDScreen screen;

    @Scheduled(fixedRate = 500)
    public void refresh() {
        Date date = new Date();
        refreshTime(date);
        refreshDate(date);
    }

    private void refreshDate(Date date) {
        String dateStr = DateUtil.formatDateAndHours(date);
        if (dateStr.equals(lastDateStr)) {
            return;
        }
        lastDateStr = dateStr;
        screen.drawString(dateStr, "宋体", 16, 15, 0);
    }

    private void refreshTime(Date date) {
        String timeStr = DateUtil.formatOnlyTime(date);
        if (timeStr.equals(lastTimeStr)) {
            return;
        }
        lastTimeStr = timeStr;
        screen.drawString(timeStr, "黑体", 48, 10, 2);
    }
}
